import logging

from selenium.common import *
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

from common.report_add_img import add_img_2_report
from config.common_driver import *

import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException,NoSuchElementException
# 封装常用的操作方法，对这些方法进行异常捕获，显示等待以及信息输出的功能
from common.find_img import FindImg
from common.report_add_img import add_img_2_report
from common.tools import get_project_path, build_path
from logs.log import log






class Base:
    def find_element_new(self, driver, locator_type, locator_value, timeout=10):
        try:
            log.info(f"正在查找元素：{locator_type} - {locator_value}")
            element = WebDriverWait(driver, timeout).until(
                EC.presence_of_element_located(
                    (locator_type, locator_value)
                )
            )
            return element
        except(TimeoutException, NoSuchElementException):
            log.warning(f"元素未找到:{locator_type},{locator_value}")
            return None

    def element_sent_keys(self, driver, locator_type, locator_value, value, timeout=10):
        try:
            log.info(f"在元素{locator_type, locator_value}中输入内容{value}")
            element = self.find_element_new(driver, locator_type, locator_value, timeout)
            element.clear()
            element.send_keys(value)
        except Exception as e:
            log.warning(f'在元素{locator_type, locator_value}中输入内容失败：{str(e)}')
            return None

    def element_click_new(self, driver, locator_type, locator_value, timeout=10):
        try:
            log.info(f"点击元素：{locator_type} - {locator_value}")
            element = self.find_element_new(driver, locator_type, locator_value, timeout)
            element.click()
        except Exception as e:
            log.warning(f"点击元素{locator_type, locator_value}失败：{str(e)}")
            return None

    def get_text(self,driver,locator_type,locator_value):
        element=self.find_element_new(driver,locator_type,locator_value)
        try:
            element_text=element.text
            return element_text
        except Exception as e:
            print(f"获取元素失败,错误信息为：{str(e)}")

class BaseAssert:
    def assert_equal_screen_shot(self, driver, exceptor, step_name):
        if not isinstance(exceptor, tuple):
            print("exceptor参数类型错误，必须传元组类型")
        else:
            print(f"正在断言：{exceptor[0]} 是否等于 {exceptor[1]}")
            try:
                assert exceptor[0] == exceptor[1]
                add_img_2_report(driver, step_name=step_name)
            except Exception as e:
                print(f"断言失败：{str(e)}")
                add_img_2_report(driver, step_name=step_name)
                raise e



    def find_img_in_source(self, driver, img_name):
        source_img_path = get_project_path() + build_path("img","source_img", img_name, add_sep_before=True)
        # 需要查找的图片的路径
        search_img_path = get_project_path() + build_path("img","assert_img", img_name, add_sep_before=True)
        # 截图并保存图片
        driver.get_screenshot_as_file(source_img_path)
        time.sleep(3)
        # 在原图中查找是否有指定的图片来返回我们的信心值
        confidence = FindImg().get_confidence(source_img_path,search_img_path)
        return confidence